package com.xiaoyu.izf.service;

import com.google.common.collect.Lists;
import com.xiaoyu.izf.model.MerchantInfo;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.model.page.DataTableBean;
import com.xiaoyu.izf.model.page.ParamListBean;
import com.xiaoyu.izf.repository.MerchantInfoDao;
import com.xiaoyu.izf.repository.MerchantPayChannelDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.sql.Timestamp;
import java.util.List;

@Service
public class MerchantInfoService extends BaseService {

    @Autowired
    private MerchantInfoDao merchantInfoDao;
    @Autowired
    private MerchantPayChannelService merchantPayChannelService;

    public MerchantInfo findById(int id){
        return merchantInfoDao.oneById(id);
    }

    public MerchantInfo findByName(String name){
        return merchantInfoDao.findByMerchantName(name);
    }

    public MerchantInfo findBySimpleName(String simpleName){
        return merchantInfoDao.findByMerchantSimpleName(simpleName);
    }

    @Transactional
    public MerchantInfo add(MerchantInfo merchantInfo,int parUid){
        merchantInfo.setDisabled(1);
        merchantInfo.setRateReturnCommission(0D);
        merchantInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
        merchantInfo.setParUid(parUid);
        merchantInfo = merchantInfoDao.save(merchantInfo);
        merchantPayChannelService.add(merchantInfo.getId());
        return merchantInfo;
    }

    public MerchantInfo findByLinkmanMobile(String mobile){
        return merchantInfoDao.findByLinkmanMobile(mobile);
    }

    public DataTableBean list(ParamListBean param, UserInfo user){
        PageRequest pageRequest = buildPageRequestSort(param.getPageNo(), param.getPageLength(), "id",param.getFieldName(),param.getCompositorType());

        //添加查询条件
        Page<MerchantInfo> page = merchantInfoDao.findAll(new Specification<MerchantInfo>() {

            /**
             * 构建查询条件（Predicate断言对象）
             * @param root entity：UserInfo
             * @param query
             * @param cb 条件构建器
             * @return
             */
            @Override
            public Predicate toPredicate(Root<MerchantInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //断言列表，可以添加多个断言
                List<Predicate> predicates = Lists.newArrayList();
                //准备结合断言链
                predicates.add(cb.conjunction());

                if (StringUtils.hasLength(param.getParam().get("parUserName"))){
                    predicates.add(
                            cb.like(root.get("parUser").get("userName").as(String.class),"%"+param.getParam().get("parUserName")+"%"));
                }

                if (StringUtils.hasLength(param.getParam().get("parNickName"))){
                    predicates.add(
                            cb.like(root.get("parUser").get("nickName").as(String.class),"%"+param.getParam().get("parNickName")+"%"));
                }

                if (StringUtils.hasLength(param.getParam().get("merchantName"))){
                    predicates.add(
                            cb.like(root.get("merchantName").as(String.class),"%"+param.getParam().get("merchantName")+"%"));
                }

                if (StringUtils.hasLength(param.getParam().get("linkmanMobile"))){
                    predicates.add(
                            cb.equal(root.get("linkmanMobile").as(String.class),param.getParam().get("linkmanMobile")));
                }

                if (StringUtils.hasLength(param.getParam().get("uid"))){
                    predicates.add(
                            cb.equal(root.get("parUid").as(Integer.class),param.getParam().get("uid")));
                }

                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        },pageRequest);
//		//无查询条件
//		Page<UserInfo> page = userInfoDao.findAll(pageRequest);

        return dataTableBean(page,param);
    }

    public MerchantInfo save(MerchantInfo merchantInfo){
        return merchantInfoDao.save(merchantInfo);
    }
}
